<%inherit file="../change_form.html" />

<%def name="block_extrahead()">
    ${parent.block_extrahead()}

    <script type="text/javascript">

        django.jQuery(function () {
            var $ = django.jQuery;

            var SummationField = function (container, classes, label, max) {
                if (!container) {
                    return;
                }

                var self = this;

                this.elements = [];

                $.each(classes, function (i, cls) {
                    $(cls + " select", container).each(function (i, e) {
                        self.elements.push(e);

                        $(e).change(function () {
                            self.update();
                        });
                    });
                });

                this.row = $("<div />").addClass("form-row");
                this.label = $("<label />").text(label);    // removed .addClass("required");
                this.content = $("<span />");
                this.sum = $("<span />");
                this.total = $("<span />").text(" out of " + (this.elements.length * max));

                this.content.append(this.sum).append(this.total);
                this.row.append($("<div />").append(this.label).append(this.content));

                this.row.insertAfter($(classes[classes.length - 1], container));
            };

            SummationField.prototype.update = function () {
                var total = 0;

                $.each(this.elements, function (i, element) {
                    try {
                        total += new Number($(element).val());
                    }
                    catch (e) {}
                });

                this.sum.text(total);
            };

            var Fatigue = function (container) {
                var classes = [
                    ".sitting_reading",
                    ".watching_tv",
                    ".sitting_inactive_public",
                    ".passenger_car",
                    ".lying_down_afternoon",
                    ".sitting_talking",
                    ".sitting_quietly_lunch",
                    ".in_car"
                ];

                SummationField.call(this, container, classes, "Fatigue Score:", 3);
            };

            Fatigue.prototype = new SummationField();

            var MuscleStrength = function (container) {
                var classes = [
                    ".flexor_digitorum_profundis",
                    ".tibialis_anterior",
                    ".neck_flexion",
                    ".iliopsoas"
                ];

                SummationField.call(this, container, classes, "Muscle Strength Score:", 5);
            };

            MuscleStrength.prototype = new SummationField();

            $("#fatigue-group > .inline-related").each(function (i, form) {
                var fatigue = new Fatigue(form);
                fatigue.update();
            });

            $("#muscle-group > .inline-related").each(function (i, form) {
                var ms = new MuscleStrength(form);
                ms.update();
            });
        });
    </script>
</%def>
